<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
    <title>Coverage Report :: AnimatedSprite</title>
    <style type="text/css">
        @import "../../.css/coverage.css";
    </style>
</head>

<body>
<div class="header"></div>

<div class="content">
    <div class="breadCrumbs">
        [ <a href="../../index.html">all classes</a> ]
        [ <a href="../index.html">nl.tudelft.jpacman.sprite</a> ]
    </div>

    <h1>Coverage Summary for Class: AnimatedSprite (nl.tudelft.jpacman.sprite)</h1>

    <table class="coverageStats">
        <tr>
            <th class="name">Class</th>
            <th class="coverageStat
">
                Class, %
            </th>
            <th class="coverageStat
">
                Method, %
            </th>
            <th class="coverageStat
">
                Line, %
            </th>
        </tr>
        <tr>
            <td class="name">AnimatedSprite</td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 1)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 11)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 38)
  </span>
            </td>
        </tr>

    </table>

    <br/>
    <br/>


    <div class="sourceCode"><i>1</i>&nbsp;package nl.tudelft.jpacman.sprite;
        <i>2</i>&nbsp;
        <i>3</i>&nbsp;import java.awt.Graphics;
        <i>4</i>&nbsp;
        <i>5</i>&nbsp;/**
        <i>6</i>&nbsp; * Animated sprite, renders the frame depending on the time of requesting the
        <i>7</i>&nbsp; * draw.
        <i>8</i>&nbsp; *
        <i>9</i>&nbsp; * @author Jeroen Roosen
        <i>10</i>&nbsp; */
        <b class="nc"><i>11</i>&nbsp;public class AnimatedSprite implements Sprite {</b>
        <i>12</i>&nbsp;
        <i>13</i>&nbsp; /**
        <i>14</i>&nbsp; * Static empty sprite to serve as the end of a non-looping sprite.
        <i>15</i>&nbsp; */
        <b class="nc"><i>16</i>&nbsp; private static final Sprite END_OF_LOOP = new EmptySprite();</b>
        <i>17</i>&nbsp;
        <i>18</i>&nbsp; /**
        <i>19</i>&nbsp; * The animation itself, in frames.
        <i>20</i>&nbsp; */
        <i>21</i>&nbsp; private final Sprite[] animationFrames;
        <i>22</i>&nbsp;
        <i>23</i>&nbsp; /**
        <i>24</i>&nbsp; * The delay between frames.
        <i>25</i>&nbsp; */
        <i>26</i>&nbsp; private final int animationDelay;
        <i>27</i>&nbsp;
        <i>28</i>&nbsp; /**
        <i>29</i>&nbsp; * Whether is animation should be looping or not.
        <i>30</i>&nbsp; */
        <i>31</i>&nbsp; private final boolean looping;
        <i>32</i>&nbsp;
        <i>33</i>&nbsp; /**
        <i>34</i>&nbsp; * The index of the current frame.
        <i>35</i>&nbsp; */
        <i>36</i>&nbsp; private int current;
        <i>37</i>&nbsp;
        <i>38</i>&nbsp; /**
        <i>39</i>&nbsp; * Whether this sprite is currently animating or not.
        <i>40</i>&nbsp; */
        <i>41</i>&nbsp; private boolean animating;
        <i>42</i>&nbsp;
        <i>43</i>&nbsp; /**
        <i>44</i>&nbsp; * The {@link System#currentTimeMillis()} stamp of the last update.
        <i>45</i>&nbsp; */
        <i>46</i>&nbsp; private long lastUpdate;
        <i>47</i>&nbsp;
        <i>48</i>&nbsp; /**
        <i>49</i>&nbsp; * Creates a new animating sprite that will change frames every interval. By
        <i>50</i>&nbsp; * default the sprite is not animating.
        <i>51</i>&nbsp; *
        <i>52</i>&nbsp; * @param frames
        <i>53</i>&nbsp; * The frames of this animation.
        <i>54</i>&nbsp; * @param delay
        <i>55</i>&nbsp; * The delay between frames.
        <i>56</i>&nbsp; * @param loop
        <i>57</i>&nbsp; * Whether or not this sprite should be looping.
        <i>58</i>&nbsp; */
        <i>59</i>&nbsp; public AnimatedSprite(Sprite[] frames, int delay, boolean loop) {
        <b class="nc"><i>60</i>&nbsp; this(frames, delay, loop, false);</b>
        <i>61</i>&nbsp; }
        <i>62</i>&nbsp;
        <i>63</i>&nbsp; /**
        <i>64</i>&nbsp; * Creates a new animating sprite that will change frames every interval.
        <i>65</i>&nbsp; *
        <i>66</i>&nbsp; * @param frames
        <i>67</i>&nbsp; * The frames of this animation.
        <i>68</i>&nbsp; * @param delay
        <i>69</i>&nbsp; * The delay between frames.
        <i>70</i>&nbsp; * @param loop
        <i>71</i>&nbsp; * Whether or not this sprite should be looping.
        <i>72</i>&nbsp; * @param isAnimating
        <i>73</i>&nbsp; * Whether or not this sprite is animating from the start.
        <i>74</i>&nbsp; */
        <b class="nc"><i>75</i>&nbsp; public AnimatedSprite(Sprite[] frames, int delay, boolean loop, boolean
            isAnimating) {</b>
        <b class="nc"><i>76</i>&nbsp; assert frames.length &gt; 0;</b>
        <i>77</i>&nbsp;
        <b class="nc"><i>78</i>&nbsp; this.animationFrames = frames.clone();</b>
        <b class="nc"><i>79</i>&nbsp; this.animationDelay = delay;</b>
        <b class="nc"><i>80</i>&nbsp; this.looping = loop;</b>
        <b class="nc"><i>81</i>&nbsp; this.animating = isAnimating;</b>
        <i>82</i>&nbsp;
        <b class="nc"><i>83</i>&nbsp; this.current = 0;</b>
        <b class="nc"><i>84</i>&nbsp; this.lastUpdate = System.currentTimeMillis();</b>
        <i>85</i>&nbsp; }
        <i>86</i>&nbsp;
        <i>87</i>&nbsp; /**
        <i>88</i>&nbsp; * @return The frame of the current index.
        <i>89</i>&nbsp; */
        <i>90</i>&nbsp; private Sprite currentSprite() {
        <b class="nc"><i>91</i>&nbsp; Sprite result = END_OF_LOOP;</b>
        <b class="nc"><i>92</i>&nbsp; if (current &lt; animationFrames.length) {</b>
        <b class="nc"><i>93</i>&nbsp; result = animationFrames[current];</b>
        <i>94</i>&nbsp; }
        <b class="nc"><i>95</i>&nbsp; assert result != null;</b>
        <b class="nc"><i>96</i>&nbsp; return result;</b>
        <i>97</i>&nbsp; }
        <i>98</i>&nbsp;
        <i>99</i>&nbsp; /**
        <i>100</i>&nbsp; * Starts or stops the animation of this sprite.
        <i>101</i>&nbsp; *
        <i>102</i>&nbsp; * @param isAnimating
        <i>103</i>&nbsp; * &lt;code&gt;true&lt;/code&gt; to animate this sprite or &lt;code&gt;false&lt;/code&gt;
        <i>104</i>&nbsp; * to stop animating this sprite.
        <i>105</i>&nbsp; */
        <i>106</i>&nbsp; public void setAnimating(boolean isAnimating) {
        <b class="nc"><i>107</i>&nbsp; this.animating = isAnimating;</b>
        <i>108</i>&nbsp; }
        <i>109</i>&nbsp;
        <i>110</i>&nbsp; /**
        <i>111</i>&nbsp; * (Re)starts the current animation.
        <i>112</i>&nbsp; */
        <i>113</i>&nbsp; public void restart() {
        <b class="nc"><i>114</i>&nbsp; this.current = 0;</b>
        <b class="nc"><i>115</i>&nbsp; this.lastUpdate = System.currentTimeMillis();</b>
        <b class="nc"><i>116</i>&nbsp; setAnimating(true);</b>
        <i>117</i>&nbsp; }
        <i>118</i>&nbsp;
        <i>119</i>&nbsp; @Override
        <i>120</i>&nbsp; public void draw(Graphics graphics, int x, int y, int width, int height) {
        <b class="nc"><i>121</i>&nbsp; update();</b>
        <b class="nc"><i>122</i>&nbsp; currentSprite().draw(graphics, x, y, width, height);</b>
        <i>123</i>&nbsp; }
        <i>124</i>&nbsp;
        <i>125</i>&nbsp; @Override
        <i>126</i>&nbsp; public Sprite split(int x, int y, int width, int height) {
        <b class="nc"><i>127</i>&nbsp; update();</b>
        <b class="nc"><i>128</i>&nbsp; return currentSprite().split(x, y, width, height);</b>
        <i>129</i>&nbsp; }
        <i>130</i>&nbsp;
        <i>131</i>&nbsp; /**
        <i>132</i>&nbsp; * Updates the current frame index depending on the current system time.
        <i>133</i>&nbsp; */
        <i>134</i>&nbsp; private void update() {
        <b class="nc"><i>135</i>&nbsp; long now = System.currentTimeMillis();</b>
        <b class="nc"><i>136</i>&nbsp; if (animating) {</b>
        <b class="nc"><i>137</i>&nbsp; while (lastUpdate &lt; now) {</b>
        <b class="nc"><i>138</i>&nbsp; lastUpdate += animationDelay;</b>
        <b class="nc"><i>139</i>&nbsp; current++;</b>
        <b class="nc"><i>140</i>&nbsp; if (looping) {</b>
        <b class="nc"><i>141</i>&nbsp; current %= animationFrames.length;</b>
        <b class="nc"><i>142</i>&nbsp; } else if (current == animationFrames.length) {</b>
        <b class="nc"><i>143</i>&nbsp; animating = false;</b>
        <i>144</i>&nbsp; }
        <i>145</i>&nbsp; }
        <i>146</i>&nbsp; } else {
        <b class="nc"><i>147</i>&nbsp; lastUpdate = now;</b>
        <i>148</i>&nbsp; }
        <i>149</i>&nbsp; }
        <i>150</i>&nbsp;
        <i>151</i>&nbsp; @Override
        <i>152</i>&nbsp; public int getWidth() {
        <b class="nc"><i>153</i>&nbsp; assert currentSprite() != null;</b>
        <b class="nc"><i>154</i>&nbsp; return currentSprite().getWidth();</b>
        <i>155</i>&nbsp; }
        <i>156</i>&nbsp;
        <i>157</i>&nbsp; @Override
        <i>158</i>&nbsp; public int getHeight() {
        <b class="nc"><i>159</i>&nbsp; assert currentSprite() != null;</b>
        <b class="nc"><i>160</i>&nbsp; return currentSprite().getHeight();</b>
        <i>161</i>&nbsp; }
        <i>162</i>&nbsp;
        <i>163</i>&nbsp;}
    </div>
</div>

<div class="footer">

    <div style="float:right;">generated on 2023-04-25 16:45</div>
</div>
</body>
</html>
